package com.gx.auth.dao;

import com.gx.auth.common.BaseDAO;
import com.gx.auth.entity.Function;
import com.gx.auth.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.*;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2017/4/4.
 */
@Slf4j
public class FunctionDAO extends BaseDAO {
    private class FunctionMapper implements RowMapper {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            Function function = new Function();
            function.setId(resultSet.getLong("id"));
            function.setName(resultSet.getString("name"));
            function.setParentId(resultSet.getLong("parent_id"));
            function.setAccordion(resultSet.getInt("accordion"));
            function.setSerialNum(resultSet.getInt("serial_num"));
            function.setUrl(resultSet.getString("url"));
            return function;
        }
    }


    public User findUserByid(int id) {
        String sql = "select * from auth_user where id = ? ";
        try {
            return (User) jdbcTemplate.queryForObject(sql, new Object[]{id}, new FunctionMapper());
        } catch (Exception e) {
            log.error("查询getUser异常：" + e.getMessage(), e);
            return null;
        }

    }

    public Function insertFunction(Function function) {
        String sql = "insert  auth_function (name,parent_id,url,serial_num,accordion) values(?,?,?,?,?) ";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        //
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, function.getName());
                ps.setLong(2, function.getParentId());
                ps.setString(3, function.getUrl());
                ps.setInt(4, function.getSerialNum());
                ps.setInt(5, function.getAccordion());
                return ps;
            }
        }, keyHolder);
        function.setId(keyHolder.getKey().longValue());
        return function;
    }

    /**
     * 更新功能URL
     *
     * @param function
     * @return
     */
    public int updataFunctionUrl(Function function) {
        String sql = "update  auth_function set url= ? where id = ?";
        return jdbcTemplate.update(sql, function.getUrl(), function.getId());

    }
    public int updataFunctionUrl(Long id ,String url) {
        String sql = "update  auth_function set url= ? where id = ?";
        return jdbcTemplate.update(sql, url, id);

    }

    /**
     * 分页查询功能  根据父节点
     *
     * @param page
     * @param size
     * @param parentld
     * @return
     */
    public List<Function> selectFunctions(int page, int size, Long parentld) {
        String sql = "select * from auth_function where parent_id=? limit ?,?";
        return jdbcTemplate.query(sql, new Object[]{parentld, (page - 1) * size, size}, new FunctionMapper());
    }

    /**
     * 根据id删除功能id
     *
     * @param id
     * @return
     */
    public int deleteById(Long id) {
        String sql = "delete from auth_function where id = ?";
        return jdbcTemplate.update(sql, id);
    }

    /**
     * 查询全部功能
     * @return
     */
    public List<Function> selectAllFunctions() {
        String sql = "select * from auth_function ";
        return jdbcTemplate.query(sql, new FunctionMapper());
    }
}
